package dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dao.TableDao;
import entiey.Table;
import util.JDBCUtil;
import util.ResultSetCreateObject;

public class TableDaoImpl implements TableDao,ResultSetCreateObject<Table> {

	@Override
	public int add(Table t) {
		int num=0;
		String sql="INSERT INTO `tables` (deskNo,icon,node) VALUES (?,?,?);";
		num=JDBCUtil.executeUpdate(sql, t.getDeskNo(),t.getIcon(),t.getNode());
		return num;
	}

	@Override
	public int delete(int no) {
		int num=0;
		num=JDBCUtil.executeUpdate("delete from tables where id=?",no);
		return num;
	}

	@Override
	public int update(Table t) {
		int num=0;
		String sql="update tables set icon=?,node=?,status=? where id=?";
		num=JDBCUtil.executeUpdate(sql,t.getIcon(),t.getNode(),t.getStatus(),t.getId());
		return num;
	}

	@Override
	public List<Table> findAll() {
		List<Table> list=new ArrayList<Table>();
		String sql="select * from tables";
		list=JDBCUtil.executeQuery(
				sql,
				new ResultSetCreateObject<Table>() {

					@Override
					public Table create(ResultSet rs) {

						Table table = new Table();
						try {
							table.setId(rs.getInt("id"));
							table.setDeskNo(rs.getInt("deskNo"));
							table.setIcon(rs.getString("icon"));
							table.setNode(rs.getString("node"));
							table.setStatus(rs.getInt("status"));
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						return table;
					}
				});
		return list;
	}

	@Override
	public Table findById(int id) {
		List<Table> list=new ArrayList<Table>();
		list=JDBCUtil.executeQuery("select * from tables where id=?", this,id);
		return list.get(0);
	}

	@Override
	public Table create(ResultSet rs) {
		
		
		Table table = new Table();
		try {
			table.setId(rs.getInt("id"));
			table.setDeskNo(rs.getInt("deskNo"));
			table.setIcon(rs.getString("icon"));
			table.setNode(rs.getString("node"));
			table.setStatus(rs.getInt("status"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return table;
	}

	@Override
	public List<Table> findBysattus(int status) {
		List<Table> list = JDBCUtil.executeQuery("select * from tables where status=?", this, status);
		return list;
	}

}
